require(['jquery','token','sweetalert','libs/vue-validate'], function($,token,swal) {
    Vue.component('custom-error', {
        props: ['field', 'validator', 'message'],
        template: '<p class="error-{{field}}-{{validator}}">{{message}}</p>'
    })
    var content=new Vue({
        el: '.content',
        validators: {
            isPhone: function (val/*,rule*/) {
                return /^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/.test(val)
            }
        },
        filters:{
           currency: {
               read: function (val) {
                   return val.toFixed(2)
               },
               // write 函数会在数据写入到模型之前被调用
               write: function (val, oldVal) {
                   var number = +val.replace(/[^\d.]/g, '')
                   return isNaN(number) ? 0 : parseFloat(number.toFixed(2))
               }
           }
        },
        data:{
            rule:{
                shop_name:{
                    required: { rule: true, message: '请输入店铺名 !!' },
                    maxlength: { rule: 8, message: '店铺名过长 !!' }
                },
                shop_desc:{
                    required: { rule: true, message: '请输入店铺描述 !!' },
                    minlength: { rule: 2, message: '店铺描述太短 !!' }
                },
                phone:{
                    required: { rule: true, message: '请输入手机号 !!' },
                    isPhone: { rule: true, message: '请输入正确的手机号码 !!' }
                },
                address:{
                    required: { rule: true, message: '请输入地址 !!' },
                },
                least_price_send:{
                    required: { rule: true, message: '请输入起送金额 !!' }
                }
            },
            least_price_send:0,
        },
        methods: {
            submit: function(){
                if(this.$validation1.invalid){
                    swal("Oops...", this.$validation1.errors[this.$validation1.errors.length-1].message, "error");
                }else {
                    var shop_name=$('#shop_name').val();
                    var shop_desc=$('#shop_desc').val();
                    var phone=$('#phone').val();
                    var address=$('#address').val();
                    var least_price_send=$('#least_price_send').val();
                    $.ajax({
                        url:'/api/shop',
                        method:'post',
                        data:{
                            shop_name:shop_name,
                            shop_desc:shop_desc,
                            phone:phone,
                            address:address,
                            least_price_send:least_price_send,
                        },
                        success:function(data){
                            //console.log(data)
                            if(data.status=="success"){
                                window.location.href='/myShop';
                            }
                        }
                    })
                }
            }
        }
    })

});